import json

n_rows = 3
n_cols = 5

# alpha = [0.1, 0.3, 0.5, 0.7, 0.9]
alpha = [0.01, 0.25, 0.50, 0.75, 0.999]
# eta = [2, 4, 16]
eta = [100, 100, 100]


scene_desc = {
    "materials": [
        {
            "type": "lambertian",
            "albedo": {
                "type": "value",
                "value": [
                    0.8,
                    0.8,
                    0.8
                ]
            }
        },
        {
            "type": "lambertian",
            "albedo": {
                "type": "value",
                "value": [
                    0.8,
                    0.0,
                    0.0
                ]
            }
        },
        {
            "type": "lambertian",
            "albedo": {
                "type": "value",
                "value": [
                    0.0,
                    0.8,
                    0.0
                ]
            }
        }
    ],
    "meshes": [
        {
            "type": "plane",
            "O": [
                -1,
                -1,
                0
            ],
            "U": [
                2,
                0,
                0
            ],
            "V": [
                0,
                2,
                0
            ]
        },
        {
            "type": "triangle",
            "source type": "obj file",
            "source path": "./assets/scenes/simple-objs/monkey.obj"
        }
    ],
    "mediums": [],
    "background": {
        "type": "solid color",
        "radiance": [
            0.0,
            0.0,
            0.0
        ]
    },
    "lights": [
        {
            "type": "plane mesh",
            "radiance": [
                4,
                4,
                4
            ]
        },
        {
            "type": "plane mesh",
            "radiance": [
                8,
                8,
                8
            ]
        }
    ],
    "camera": {
        "type": "perspective",
        "fov": 36,
        "znear": 1e-2,
        "zfar": 1e2,
        "aspect ratio": 1.0,
        "origin": [
            8.0,
            0.0,
            0.0
        ],
        "lookat": [
            0.0,
            0.0,
            0.0
        ],
        "up": [
            0.0,
            0.0,
            1.0
        ]
    },
    "instances": [
        {
            "type": "static",
            "children": [
                {
                    "name": "地板",
                    "type": "gas",
                    "mesh id": 0,
                    "material": 0,
                    "transform": [
                        2,
                        0,
                        0,
                        0,
                        0,
                        2,
                        0,
                        0,
                        0,
                        0,
                        2,
                        -2,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "天花板",
                    "type": "gas",
                    "mesh id": 0,
                    "material": 0,
                    "transform": [
                        2,
                        0,
                        0,
                        0,
                        0,
                        2,
                        0,
                        0,
                        0,
                        0,
                        -2,
                        2,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "白墙",
                    "type": "gas",
                    "mesh id": 0,
                    "material": 0,
                    "transform": [
                        0,
                        0,
                        2,
                        -2,
                        0,
                        2,
                        0,
                        0,
                        2,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "红墙",
                    "type": "gas",
                    "mesh id": 0,
                    "material": 1,
                    "transform": [
                        2,
                        0,
                        0,
                        0,
                        0,
                        0,
                        2,
                        -2,
                        0,
                        2,
                        0,
                        0,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "绿墙",
                    "type": "gas",
                    "mesh id": 0,
                    "material": 2,
                    "transform": [
                        2,
                        0,
                        0,
                        0,
                        0,
                        0,
                        -2,
                        2,
                        0,
                        -2,
                        0,
                        0,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "灯1",
                    "type": "gas",
                    "mesh id": 0,
                    "light": 0,
                    "transform": [
                        0.5,
                        0,
                        0,
                        0,
                        0,
                        0.5,
                        0,
                        0,
                        0,
                        0,
                        -0.5,
                        1.99,
                        0,
                        0,
                        0,
                        1
                    ]
                },
                {
                    "name": "灯2",
                    "type": "gas",
                    "mesh id": 0,
                    "light": 1,
                    "transform": [
                        0,
                        0,
                        -0.5,
                        10,
                        0,
                        0.5,
                        0,
                        0,
                        0.5,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        1
                    ]
                }
            ]
        }
    ],
    "integrator": {
        "type": "path",
        "max depth": 8
    },
    "renderer": {
        "spp": 1,
        "tmin": 0,
        "tmax": 0,
        "width": 512,
        "height": 512
    }
}


curr_mat_id = 3
for r in range(n_rows):
    row_off = r - (n_rows) // 2
    for c in range(n_cols):
        col_off = c - (n_cols) // 2
        mat = {
            "type": "ggx",
            "alpha": {
                "type": "value",
                "value": alpha[c]
            },
            "eta": eta[r],
            "albedo": {
                "type": "value",
                "value": [1.0, 1.0, 1.0]
            }
        }
        instance = {
            "name": f"ggx monkey row-{r} col-{c}",
            "type": "gas",
            "mesh id": 1,
            "material": curr_mat_id,
            "transform": [
                0.25, 0,       0, -0.60 * row_off,
                0,    0.25,    0,  0.75 * col_off,
                0,    0,    0.25,  0.75 * row_off,
                0,    0,       0,     1
            ]
        }
        scene_desc["materials"].append(mat)
        scene_desc["instances"][0]["children"].append(instance)
        curr_mat_id += 1

with open("./assets/scenes/ggx-monkey-grid/#scene_desc.json", "w") as f:
    json.dump(scene_desc, f)
